﻿@typeparam TItem
@inherits _BaseDataGridModal<TItem>
<Modal Visible="@PopupVisible" Closed="@Cancel">
    <ModalBackdrop />
    <ModalContent Size="@PopupSize">
        <ModalHeader>
            <ModalTitle>
                @if ( TitleTemplate != null && EditItem != null )
                {
                    @TitleTemplate( new PopupTitleContext<TItem>( EditItem, EditState ) )
                }
                else
                {
                    @(EditState == DataGridEditState.Edit ? "Row Edit" : "Row Create")
                }
            </ModalTitle>
            <CloseButton Clicked="@Cancel" />
        </ModalHeader>
        <ModalBody>
            <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged">
                @if ( ParentDataGrid?.ShowValidationsSummary ?? true )
                {
                    <ValidationSummary Label="@ParentDataGrid?.ValidationsSummaryLabel" />
                }
                <Fields>
                    @foreach ( var column in Columns )
                    {
                        @if ( column.ColumnType == DataGridColumnType.Command )
                            continue;

                        @if ( column.Editable )
                        {
                            <Field ColumnSize="@column.PopupFieldColumnSize" Padding="Blazorise.Padding.Is2.OnX.Is2.OnY">
                                <Field>
                                    <FieldLabel>
                                        @column.Caption
                                    </FieldLabel>
                                    <FieldBody>
                                        <_DataGridCell TItem="TItem" Column="@column" Item="@EditItem" CellEditContext="@EditItemCellValues[column.ElementId]" ShowValidationFeedback="@(ParentDataGrid?.ShowValidationFeedback ?? false)" />
                                    </FieldBody>
                                </Field>
                            </Field>
                        }
                    }
                </Fields>
            </Validations>
        </ModalBody>
        <ModalFooter>
            @if ( ParentDataGrid?.CommandColumn?.SaveCommandAllowed == true )
            {
                @if ( ParentDataGrid?.CommandColumn?.SaveCommandTemplate != null )
                {
                    @ParentDataGrid.CommandColumn.SaveCommandTemplate( new CommandContext<TItem>
                    {
                        Clicked = callbackFactory.Create( this, SaveWithValidation ),
                        Item = EditItem,
                    } )
                }
                else
                {
                    <Button Color="Color.Link" Clicked="@SaveWithValidation">Save</Button>
                }
            }
            @if ( ParentDataGrid?.CommandColumn?.CancelCommandAllowed == true )
            {
                @if ( ParentDataGrid?.CommandColumn?.CancelCommandTemplate != null )
                {
                    @ParentDataGrid.CommandColumn.CancelCommandTemplate( new CommandContext<TItem>
                    {
                        Clicked = Cancel,
                        Item = EditItem,
                    } )
                }
                else
                {
                    <Button Color="Color.Link" Clicked="@Cancel">Cancel</Button>
                }
            }
        </ModalFooter>
    </ModalContent>
</Modal>